package mpcs.model
{
	import mpcs.NotiConst;
	import mpcs.model.vo.GlobalVO;
	import mpcs.mylibs.data.SimpleCmd;
	import mpcs.mylibs.data.SimpleMsg;
	import mpcs.mylibs.events.GenericEvent;
	import mpcs.mylibs.interfaces.IConnection;
	import mpcs.mylibs.manager.ConManager;
	import mpcs.utils.TrimUtils;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import spark.utils.DataItem;
	
	/**
	 *  
	 * @author zhangzuoqiang
	 */	
	public class HistoryProxy extends Proxy {
		
		public static const NAME:String = "HistoryProxy";
		
		private var con:IConnection = ConManager.connection;
		
		public function HistoryProxy() {
			super(NAME);
			
			con.addEventListener("500301", rPositionList);
		}
		
		// 客户请求历史数据
		public function sPositionList(mobile:String):void{
			trace("HistoryProxy ...  sPositionList ... ");
			var cmd:SimpleCmd = new SimpleCmd(100301);
			cmd.writeString(mobile);
			cmd.writeString("历史数据请求。。。");
			con.send(cmd);
		}
		
		// 历史数据请求返回
		private function rPositionList(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
				trace("HistoryProxy ... rPositionList ... ");
				// 读取历史位置列表长度
				var num:int = msg.readInt();
				trace("历史位置列表长度 = " + num );
				// 循环读取用户绑定的手机列表
				for(var i:int = 0 ; i < num ; i++){
					var item:DataItem = new DataItem();
					item.date = TrimUtils.Trim(msg.readString());
					item.longitude = String(msg.readFloat());
					item.latitude = String(msg.readFloat());
					GlobalVO.userVO.getPositonList().addItemAt(item, i);
				}
				trace("rPositionList ... GlobalVO.userVO.getPositonList().length = " + GlobalVO.userVO.getPositonList().length );
				sendNotification(NotiConst.R_POSITION_LIST_SUCCESS);
			}else{
				sendNotification(NotiConst.R_POSITION_LIST_FAILED, msg.getHeadDatas());
			}
		}
		
	}
}